package com.qu.waibao.search;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.qu.waibao.AppConfig;
import com.qu.waibao.R;
import com.qu.waibao.base.BaseActivity;
import com.qu.waibao.base.BaseRecyclerAdapter;
import com.qu.waibao.bean.SearchHome;
import com.qu.waibao.home.activity.PublishDetailActivity;
import com.qu.waibao.interf.XnetCallBack;
import com.qu.waibao.network.ActionUtil;
import com.qu.waibao.network.NetManager;
import com.qu.waibao.utils.GsonUtil;
import com.qu.waibao.widget.RecyclerRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeSearchActivity extends BaseActivity implements
        RecyclerRefreshLayout.SuperRefreshLayoutListener,BaseRecyclerAdapter.OnItemClickListener,
        View.OnClickListener{
    @BindView(R.id.mRecyclerRefreshLayout)
    RecyclerRefreshLayout mRecyclerRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.et_search)
    EditText et_search;
    private HomeSearchAdapter adapter;
    private String keywords = "";
    private int pageNum = 1;
    private boolean isRefreshing = true;
    @Override
    protected boolean initBundle(Bundle bundle) {
        if(bundle != null){
            keywords = bundle.getString("keywords","");
        }
        return super.initBundle(bundle);
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_home_search;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mRecyclerRefreshLayout.setColorSchemeResources(R.color.colorPrimaryDark);
        mRecyclerRefreshLayout.setSuperRefreshLayoutListener(this);
        et_search.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(keyCode==KeyEvent.KEYCODE_SEARCH && event.getAction()==KeyEvent.ACTION_UP){
                    keywords = et_search.getText().toString().trim();
                    if(TextUtils.isEmpty(keywords)){
                        keywords = "";
                    }
                    isRefreshing = true;
                    requestData();
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new HomeSearchAdapter(this, BaseRecyclerAdapter.ONLY_FOOTER);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
        if(adapter.getItems().size() == 0){
           mRecyclerRefreshLayout.setRefreshing(true);
           onRefreshing();
        }
        adapter.setOnItemClickListener(this);
    }

    public void requestData(){
        Map<String,String> map = new HashMap<>();
        map.put("action", ActionUtil.service_searchlist_get);
        map.put("userid", AppConfig.getString("userid"));
        map.put("keyword",keywords);
        map.put("pagenum",pageNum+"");
        NetManager.getInstance().request(this, map, true, new XnetCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                SearchHome searchHome = GsonUtil.createGson().fromJson(result,SearchHome.class);
                if(isRefreshing){
                    adapter.clear();
                    adapter.addAll(searchHome.getService());
                    if(searchHome.getService().size() < 12){
                        adapter.setState(BaseRecyclerAdapter.STATE_NO_MORE,true);
                    }
                }else {
                    adapter.addAll(searchHome.getService());
                    if(searchHome.getService().size() < 12){
                        adapter.setState(BaseRecyclerAdapter.STATE_NO_MORE,true);
                    }
                    if(searchHome.getService().size() == 0){
                        pageNum--;
                    }
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
               if(pageNum > 1){
                   pageNum--;
                   onComplate();
               }
            }

            @Override
            public void onFinished() {
              onComplate();
            }
        });
    }

    @Override
    public void onRefreshing() {
        isRefreshing = true;
        pageNum = 1;
        requestData();
    }

    @Override
    public void onLoadMore() {
        isRefreshing = false;
        pageNum++;
        adapter.setState(BaseRecyclerAdapter.STATE_LOAD_MORE,true);
        requestData();
    }

    public void onComplate(){
        mRecyclerRefreshLayout.onComplete();
        isRefreshing = false;
    }

    @Override
    public void onItemClick(int position, long itemId) {
        String serviceid = adapter.getItems().get(position).getServiceid();
        Intent intent = new Intent(this,PublishDetailActivity.class);
        intent.putExtra("serviceid",serviceid);
        startActivity(intent);
    }
    @OnClick({R.id.ll_back,R.id.tv_search})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_back:
                finish();
                break;
            case R.id.tv_search:
                keywords = et_search.getText().toString();
                if(!TextUtils.isEmpty(keywords)){
                    isRefreshing = true;
                    requestData();
                }
                break;
        }
    }
}
